package com.ahstu.learning.phoneContacts;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

public class ContactManager {
    private static final String[] phone_projection = new String[]{Phone.DISPLAY_NAME, Phone.NUMBER};

    public static List<ContactBean> getContacts(Context context) {

        List<ContactBean> lists = new ArrayList<ContactBean>();
        //phoneInfo phone=new phoneInfo();
        ContentResolver resover = context.getContentResolver();

        Cursor cursor = resover.query(Phone.CONTENT_URI, phone_projection, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String phonenumber = cursor.getString(1);
                if (TextUtils.isEmpty(phonenumber))
                    continue;

                String name = cursor.getString(0);
                ContactBean phone = new ContactBean();
                phone.setName(name);
                phone.setPhone(phonenumber);
                lists.add(phone);
            }

            cursor.close();
        }
        Uri uri = Uri.parse("content://icc/adn");
        Cursor cur = resover.query(uri, phone_projection, null, null, null);
        if (cur != null) {
            while (cur.moveToNext()) {
                String phonenumber = cur.getString(1);
                if (TextUtils.isEmpty(phonenumber))
                    continue;

                String name = cur.getString(0);
                ContactBean phone = new ContactBean();
                phone.setName(name);
                phone.setPhone(phonenumber);
                lists.add(phone);
            }

            cur.close();
        }
        return lists;

    }

    public static void addContact(Context context, ContactBean contact) {
        ContentResolver resolver = context.getContentResolver();

        ContentValues values = new ContentValues();
        Uri rawContactUri = resolver.insert(RawContacts.CONTENT_URI, values);
        long rawContactId = ContentUris.parseId(rawContactUri);

        ContentValues valuesData1 = new ContentValues();
        valuesData1.put(Data.RAW_CONTACT_ID, rawContactId);
        valuesData1.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
        valuesData1.put(Phone.NUMBER, contact.getPhone());
        resolver.insert(Data.CONTENT_URI, valuesData1);

        ContentValues valuesData2 = new ContentValues();
        valuesData2.put(Data.RAW_CONTACT_ID, rawContactId);
        valuesData2.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
        valuesData2.put(StructuredName.DISPLAY_NAME, contact.getName());
        resolver.insert(Data.CONTENT_URI, valuesData2);

    }

    public static void updateContact(Context context, ContactBean contact) {
        ContentResolver resolver = context.getContentResolver();

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

        ops.add(ContentProviderOperation
                .newUpdate(Data.CONTENT_URI)
                .withSelection(
                        Data.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?",
                        new String[]{
                                String.valueOf(contact.getRawContactId()),
                                StructuredName.CONTENT_ITEM_TYPE})
                .withValue(StructuredName.DISPLAY_NAME, contact.getName())
                .build());

        ops.add(ContentProviderOperation
                .newUpdate(Data.CONTENT_URI)
                .withSelection(
                        Data.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?",
                        new String[]{
                                String.valueOf(contact.getRawContactId()),
                                Phone.CONTENT_ITEM_TYPE})
                .withValue(Phone.NUMBER, contact.getPhone()).build());
        try {
            resolver.applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            e.printStackTrace();
        }

    }

    public static void deleteContact(Context context, ContactBean contact) {
        ContentResolver resolver = context.getContentResolver();
        resolver.delete(RawContacts.CONTENT_URI, RawContacts._ID + "=?",
                new String[]{String.valueOf(contact.getRawContactId())});
    }

}
